#include "navigationmw.h"
#include "ui_navigationmw.h"
#include <QDebug>

navigationMW::navigationMW(int endId,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::navigationMW)
{
    ui->setupUi(this);

    // 设置按钮图标
//    ui->btn_navigation->setIcon(QIcon(":/pic/navigation.png"));
//    ui->btn_back->setIcon(QIcon(":/pic/back.png"));
    // 连接起点下拉列表的信号到槽函数
    connect(ui->comboBox_start, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &navigationMW::onStartComboBoxChanged);
//    QPixmap pixmap(":/1.bmp");
//    QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
//    ui->label_result->setPixmap(scaledPixmap);


    // 填充模型
   QStringList Items = {"仿古商业街", "实验实训大楼", "体育馆", "五号教学楼","学海悟道", "图书馆", "三号教学楼", "综合楼","艺体楼"};
   for (int i = 0; i < Items.size(); ++i)
   {
       //创建QStandardItem模型中的项
       QStandardItem *item1 = new QStandardItem(Items[i]);
       QStandardItem *item2 = new QStandardItem(Items[i]);
//       QString picpath = QString(":/pic/%1.png").arg(i+1);

//       item1->setIcon(QIcon(picpath));  // 设置正常状态的图标
//       item2->setIcon(QIcon(picpath));  // 设置正常状态的图标
       startModel.appendRow(item1);
       endModel.appendRow(item2);
   }
    // 设置模型到下拉列表
    ui->comboBox_start->setModel(&startModel);
    ui->comboBox_end->setModel(&endModel);
    // 设置起点下拉列表的默认项
    if (endId >= 0 && endId < endModel.rowCount()) {
        ui->comboBox_end->setCurrentIndex(endId);
    }

}

navigationMW::~navigationMW()
{
    delete ui;
}

//禁用与起点相同的选项
void navigationMW::onStartComboBoxChanged(int index)
{
    //先启用endModel所有项
    for (int i = 0; i < endModel.rowCount(); ++i)
    {
        QStandardItem *item = endModel.item(i);
        if (item)
        {
            item->setFlags(item->flags() | Qt::ItemIsEnabled);
        }
    }

    // 禁用相同索引的项，并设置暗色图标
    QStandardItem *startItem = startModel.item(index);
    QStandardItem *endItem = endModel.item(index);
    if (startItem && endItem)
    {
        // 禁用终点下拉列表中相同索引的项
        endItem->setFlags(endItem->flags() & ~Qt::ItemIsEnabled);
        // 设置禁用状态的图标
//        QIcon icon = endItem->icon();
//        QString picpath = QString(":/pic/%1.png").arg(index+1);
//        // 设置暗色图标
//        icon.addPixmap(QPixmap(picpath), QIcon::Disabled);
//        endItem->setIcon(icon);
    }
}

//返回
void navigationMW::on_btn_back_clicked()
{
    //所以可以利用此函数获取当前窗口的父窗口对象指针
    QWidget *parent = this->parentWidget();
    parent->show();//把之前父界面的隐藏状态改为显示状态，显示父界面
    this->close();//关闭当前界面
}

void navigationMW::on_btn_navigation_clicked()
{
    QString startPos = ui->comboBox_start->currentText();
    QString endPos = ui->comboBox_end->currentText();
    qDebug() << "startPos: " << startPos;
    qDebug() << "endPos: " << endPos;
    // 检查起点和终点是否相同，相同弹出警告
    if (startPos == endPos) {
      QMessageBox::warning(this, "警告", "起点和终点不能相同！");
      return; // 阻止进一步的导航操作
    }
    if (startPos == QString("仿古商业街") && endPos == QString("实验实训大楼"))
    {
        QPixmap pixmap(":/pic/1-2.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("实验实训大楼") && endPos == QString("仿古商业街"))
    {
        QPixmap pixmap(":/pic/2-1.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("体育馆"))
    {
        QPixmap pixmap(":/pic/1-3.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("体育馆") && endPos == QString("仿古商业街"))
    {
        QPixmap pixmap(":/pic/3-1.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("五号教学楼"))
    {
        QPixmap pixmap(":/pic/1-4.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("学海悟道"))
    {
        QPixmap pixmap(":/pic/1-5.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("图书馆"))
    {
        QPixmap pixmap(":/pic/1-6.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("三号教学楼"))
    {
        QPixmap pixmap(":/pic/1-7.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("综合楼"))
    {
        QPixmap pixmap(":/pic/1-8.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
    else if(startPos == QString("仿古商业街") && endPos == QString("艺体楼"))
    {
        QPixmap pixmap(":/pic/1-9.bmp");
        QPixmap scaledPixmap = pixmap.scaled(ui->label_result->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_result->setPixmap(scaledPixmap);
    }
}

